home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / RxSocket / Examples / http.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  2001-09-18  |  3.2 KB  |  136 lines

  1. /*
  2. Name:        http.rexx
  3. Description: http downloader with resume support.
  4. Usage:       rx http <url> [range]
  5. */
  6.  
  7. signal on break_c
  8.  
  9. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  10.  
  11. prg=ProgramName("NOEXT")
  12. if ~Open("STDERR","CONSOLE:","W") then SDTERR="STDOUT"
  13.  
  14. if AddLibrary("rexxsupport.library","rxsocket.library","rxlibnet.library")~=0 then
  15.     call err "Can't find" result,1
  16.  
  17. if ~RMH_ReadArgs("URL/A,RANGE/K") then do
  18.     call PrintFault()
  19.     exit
  20. end
  21.  
  22. url=parm.0.value
  23. if upper(left(url,7))~="HTTP://" then url = "http://"url
  24. if ~ParseURI(url,"u") then call err "invalid url",1
  25.  
  26. call info prg"/1.0 © alfie"
  27. call info "Host:" u.hostname
  28. call info "Port:" u.port
  29. call info "File:" u.query
  30.  
  31. call info "Resolving host addr..."
  32. sin.addraddr=resolve(u.hostname)
  33. if sin.addraddr=-1 then call err "Host <"u.hostname"> not found",1
  34. sin.addrport=u.port
  35.  
  36. sock=socket("INET","STREAM")
  37. if sock=-1 then call err "Can't create socket"
  38.  
  39. call info "Connecting..."
  40. if connect(sock,"SIN")<0 then call err "Can't connect"
  41.  
  42. fin="D0A"x
  43.  
  44. if u.path="" then request="GET /"
  45. else request="GET" u.path
  46. if u.query~="" then  request= request || "?"u.query
  47. request = request "HTTP/1.0"fin
  48. request = request || "User-agent: http.rexx" || fin
  49. request = request || "Host:" u.HostInfo || fin
  50. if parm.1.flag then request = request"Range: bytes="parm.1.value||fin
  51. if u.user~="" & u.password~="" then do
  52.     call encodeb64(u.user":"u.password,auth,"string var")
  53.     request = request || "Authorization: Basic" auth || fin
  54.     call info "User:" u.user
  55.     call info "Pass:" u.password
  56.     call info "Auth:" auth
  57. end
  58. request = request||fin
  59.  
  60. call info "Sending request..."
  61. if send(sock,request)<0 then call err "Error sending"
  62.  
  63. call info "Receiving results..."
  64. sel.read.0=sock
  65. buf=""
  66. p=0
  67. do while p=0
  68.  
  69.     res = WaitSelect("sel")
  70.     if res<0 then call err "Error in select()"
  71.     if ~sel.0.read then iterate /* no read event on s */
  72.  
  73.     res=recv(sock,"B",1024)
  74.     if res=0 then call err "Link closed",1
  75.     do while p=0 & res>0
  76.         buf=buf||b
  77.         res=recv(sock,"B",1024)
  78.         p=pos("0D0A"x,buf)
  79.     end
  80.     if res<0 then call err "Error reading"
  81.     buf=buf||b
  82. end
  83.  
  84. if p=1 then call err "Empty answer",1
  85. parse var buf head "0D0A"x buf
  86. if words(head)<3 then call err "Bad answer",1
  87. parse var head h " " code " " reasone
  88.  
  89. call info "----------------"
  90. call info head
  91. do k=0 while line~=""
  92.     parse var buf line "0D0A"x buf
  93.     if line="" then iterate
  94.     call info line
  95. end
  96. call info "----------------"
  97.  
  98. if code~=200 & code~=206 then call err "Error from server: <"code reasone">" ,1
  99.  
  100. call info "Receiving file..."
  101.  
  102. do forever
  103.  
  104.     call WriteCH("STDOUT",buf)
  105.  
  106.     res = WaitSelect("sel")
  107.     if res<0 then call err "Error in select()"
  108.     if ~sel.0.read then iterate /* no read event on s */
  109.  
  110.     res=recv(sock,"BUF",1024)
  111.     if res=0 then leave
  112.     do while res>0
  113.         call WriteCH("STDOUT",buf)
  114.         res=recv(sock,"BUF",1024)
  115.     end
  116.     /*if res<0 & errno()~=35 then call err "Error reading"*/
  117.     if res<0 then call err "Error reading"
  118. end
  119. call info "Done."
  120. exit
  121.  
  122. err:
  123. parse arg msg,ntdoerr
  124.     if ntdoerr~=1 then msg=msg "("errorstring(errno())")"
  125.     call info(msg".")
  126.     exit
  127.  
  128. info:
  129. parse arg msg
  130.     call writeln(STDERR,msg)
  131.     return
  132.  
  133. break_c:
  134.     call info "user break."
  135.     exit
  136.